<!-- 全局顶部导航 -->
<template>
  <a-row class="global-header" align="center" :warp="false">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        :selected-keys="selectedKey"
        @menu-item-click="doMenuClick"
      >
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '38px' }"
          disabled
        >
          <div class="title-bar">
            <img src="../assets/logo.png" class="logo" />
            <div class="title">OJ判题</div>
          </div>
        </a-menu-item>
        <a-menu-item :key="route.path" v-for="route in visableRoute">{{
          route.name
        }}</a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="100px">
      <div>{{ store.state.user?.loginUser?.userName || "未登录" }}</div>
    </a-col>
  </a-row>
</template>

<script setup>
import { useRouter } from "vue-router";
import { routes } from "../router/routes";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import accessEnum from "@/access/accessEnum";

const router = useRouter();
const store = useStore();
// const loginUser = computed(() => store.state.user.loginUser);

const visableRoute = computed(() => {
  return routes.filter((item) => {
    if (item.meta?.hideInMenu) {
      return false;
    }

    if (!checkAccess(store.state.user.loginUser, item?.meta?.access)) {
      return false;
    }
    return true;
  });
});
const selectedKey = ref(["/"]);
const doMenuClick = (key) => {
  router.push(key);
};

router.afterEach((to) => {
  selectedKey.value = [to.path];
});

setTimeout(() => {
  store.dispatch("user/getLoginUser", {
    userName: "SumFun5",
    userRole: accessEnum.Admin,
  });
}, 2000);
</script>

<style scoped>
.global-header {
}

.title-bar {
  display: flex;
  align-items: center;
}
.logo {
  height: 48px;
}
.title {
  color: #444;
  margin-left: 14px;
}
</style>
